   var type = 'edit';
   var ctx = $('#ctx').val();
   function addRow(){
	   if(isEditRow()){
		   $.messager.alert('系统警告','还在编辑状态，无法新增','warning');
		   return;
	   }
	   type = 'add';
	   $('#dg').datagrid('insertRow',{
		   index:0,
		   row:{}
	   });
	   $('#dg').datagrid('beginEdit',0);
   }
   
   function editRow(target){
	   if(isEditRow()){
		   $.messager.alert('系统警告','还在编辑状态，无法编辑','warning');
		   return;
	   }
	   type = 'edit';
	   var index = getRowIndex(target);
	   $('#dg').datagrid('beginEdit',getRowIndex(target));
	   //var ed = $('#dg').datagrid('getEditor', {index:index,field:'loginName'});
	   //$(ed.target).attr("disabled",true);
   }
   
   function delRow (target){
	   if(isEditRow()){
		   $.messager.alert('系统警告','还在编辑状态，无法删除','warning');
		   return;
	   }
	   $.messager.confirm('系统警告','真的要删除该条记录?'
	   ,function(r){
		   if(r){
			   var index = getRowIndex(target);
			   $('#dg').datagrid('selectRow',index);
			   var row = $('#dg').datagrid('getSelected');
			   var b = saveData(ctx+'/dic/remove',row);
			   if(b){
			   		$('#dg').datagrid('deleteRow',getRowIndex(target));
			   }
			   
		   }
		   
	   });
   }
   
   
   function editAction(index){
		//刷新指定行
		$('#dg').datagrid('refreshRow',index);
		$('#dg').datagrid('updateRow',{
			index:index,
			row:{}
		});
   }
   //获取当前编辑行的下标值
   function getRowIndex(target){
	   var tr = $(target).closest('tr.datagrid-row');
	   var index = parseInt(tr.attr('datagrid-row-index'));
	   return index;
   }
   //取消按钮
   function cancelRow(target){
	   var index = getRowIndex(target);
	   $('#dg').datagrid('endEdit',index);
	   if('add'==type){
		   $('#dg').datagrid('deleteRow',index);
	   }
   }
   
   function saveRow(target){
	   var index = getRowIndex(target);
	   $('#dg').datagrid('endEdit',index);
	   
	   $('#dg').datagrid('selectRow',index);//选中当前行
	   var row =  $('#dg').datagrid('getSelected');
	   var b = saveData(ctx+'/dic/'+type,row);
	   if(!b){
	   		$('#dg').datagrid('beginEdit',index);
	   }
	   
   }
   
   function saveData(url,data){
	   var b = false;
	   $.ajax({
		   type:'post',
		   url:url,
		   data:data,
		   dataType:'json',
		   success:function(r){
			   $.messager.alert('系统提示',r.msg,'info');
			   b = r.sus;
			   if(b){
				   $('#dg').datagrid('reload');
			   }
		   }
	   });
	   return b;
   }
   
   function isEditRow(){
	   var rows = $('#dg').datagrid('getRows');
	   for(var i=0;i<rows.length;i++){
		   var row = rows[i];
		   if(row.editing){
			   return true;
		   }
	   }
	   return false;
   }
       
         